# coding=utf-8
import zmail, time
from common.caseLog import Log
from common.readConfig import read
from common.result_code import ResultCode

class ZmailSend():
    def __init__(self):
        self.send_mail = read('email', 'username')
        self.send_mail_psw = read('email', 'password')
        self.log = Log()

    def __conne(self):
        # 链接zmail
        try:
            server = zmail.server(
                self.send_mail,
                self.send_mail_psw,
            )
            self.log.debug("邮件登录成功")
            return server
        except Exception as e:
            self.log.error("连接失败%s" % e)


    def send(self, from_mail, msg, cc=None):
        # 邮件内容
        server = self.__conne()
        # 发送邮件
        try:
            server.send_mail(from_mail, msg, cc)
            self.log.debug('发送成功')
        except Exception as e:
            self.log.error(e)

    def laster(self):
        #检索邮件
        server = self.__conne()
        laster_mail = server.get_latest()
        zmail.show(laster_mail)

    def zmail_test(self):
        server = self.__conne()
        if server.smtp_able():
            pass
        # SMTP function.
        if server.pop_able():
            pass
        # POP function.

if __name__ == '__main__':
    mail = ZmailSend()
    # mail = ZmailSend('616387521@qq.com', 'shaobo679091...')
    file_path = r"E:\TestAPIPython\perapitest\report\2020-09-09 15-28-43.html"
    message = ResultCode(file_path)
    html = '''<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ffdfd</title>
        <img src="http://hua-cloud.cn/images/logo.png" alt="" style="text-align: center" width="">
        <style>
            body {
                background-size: 1960px 1040px;
            }
            h1 {
                text-align: center;
                color: #cc0000;
            }
            .textdiv{
                text-align: center;
            }
            .ptext{
                text-align: center;
                color: #1ab394;
                font-size: 25px
            }
            .ptext1{
                text-align: center ;
                color: red;
                font-size: 25px
            }
        </style>
    </head>
    <body background="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2538026662,1778507730&fm=26&gp=0.jpg">

        <h1 class="textClass" >测试结果如下：</h1>
            <table class="textdiv">
                <p class="ptext">用例总数：%s</p>
                <p class="ptext">用例通过：%s</p>
                <p class="ptext">%s</p>
                <p class="ptext1">用例失败：%s</p>
                <p class="ptext1">%s</p>
                <p class="ptext">测试时间：%s</p>
            </table>
    </body>
</html>''' % (message.testAll, message.testPass, message.success_rate(), message.testFail, message.error_rate(), message.beginTime)
    time_str = time.strftime("%Y%m%d-%S")
    msg = {
        'subject': '{time_str}权限中心接口自动化测试邮件!'.format(time_str=time_str),
        'content_html': html,
       # 'attachments': [r'E:\hyzs\perApiTest\logs\2020_05_19.log'], # attachments发送带附件的右键，只需要讲附件的地址赋给就好
    }
    test_mail = ['616387521@qq.com']
    test_mail_cc = ['15309215554@163.com']
    mail.send(test_mail, msg=msg, cc=test_mail_cc)
    # mail.send(test_mail, msg)